package com.cet.programmercarl.algorithmperformancanalysis.双指针法.移除元素;

/**
 * @program: algorithm
 * @description: 删除升序数组的重复项
 * @author: 陈恩涛
 * @create: 2022-03-23 21:03
 **/
public class LC26 {

    /**
     *
     * @param nums
     * @return 返回去重数组元素长度
     */
    public int removeDuplicates(int[] nums) {
        int slowIdex = 0;
        // 快指针去寻找哪些元素需要保留
        for (int fastIndex = 1; fastIndex < nums.length; fastIndex++) {
            if (nums[slowIdex] != nums[fastIndex]) {
                nums[++slowIdex] = nums[fastIndex];
            }
        }
        return slowIdex + 1;
    }

    public static void main(String[] args) {
        int[] num = {1, 2, 2, 3, 4, 4, 5};
        System.out.println(new LC26().removeDuplicates(num));
    }
}
